package com.example.baseweb.controller;

import com.example.baseweb.common.ApiResponse;
import com.example.baseweb.common.PageResponse;
import com.example.baseweb.dto.OrderDetailDTO;
import com.example.baseweb.entity.OrderInfo;
import com.example.baseweb.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    // 获取所有订单（分页）
    @PostMapping("/page")
    public ApiResponse<PageResponse<OrderInfo>> getOrdersPage(@RequestBody Map<String, Object> params) {
        int page = params.get("page") != null ? Integer.valueOf(params.get("page").toString()) : 1;
        int size = params.get("size") != null ? Integer.valueOf(params.get("size").toString()) : 10;
        String userName = params.get("userName") != null ? params.get("userName").toString() : null;
        String status = params.get("status") != null ? params.get("status").toString() : null;
        return ApiResponse.success(orderService.getOrderPage(page, size, userName, status));
    }
    
    // 根据ID获取订单
    @PostMapping("/detail")
    public ApiResponse<OrderInfo> getOrderById(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        return ApiResponse.success(orderService.getOrderById(id));
    }
    
    // 获取订单详情（包含明细）
    @PostMapping("/orderDetail")
    public ApiResponse<OrderDetailDTO> getOrderDetail(@RequestBody Map<String, Object> params) {
        Long orderId = Long.valueOf(params.get("orderId").toString());
        return ApiResponse.success(orderService.getOrderDetail(orderId));
    }
    
    // 更新订单状态
    @PostMapping("/updateStatus")
    public ApiResponse<OrderInfo> updateOrderStatus(@RequestBody Map<String, Object> params) {
        Long orderId = Long.valueOf(params.get("orderId").toString());
        String status = params.get("status").toString();
        return ApiResponse.success(orderService.updateOrderStatus(orderId, status));
    }
    
    // 退款操作
    @PostMapping("/refund")
    public ApiResponse<OrderInfo> refundOrder(@RequestBody Map<String, Object> params) {
        String orderNo = params.get("orderNo").toString();
        return ApiResponse.success(orderService.refundOrder(orderNo));
    }
} 